<div id="resultBasic">
<?php
	include "../common/init.php";
	function getById($id){
		$sqlfunc = "SELECT * FROM ".DB_FIX."hang_sx WHERE id=$id and hien_thi=1";
		$resultfunc = mysql_query($sqlfunc);
		while($rowfunc = mysql_fetch_array($resultfunc))
			return $rowfunc;
	}
	$link = mysql_connect(@constant('HOST_NAME'),@constant('USER_NAME'),@constant('PASS'));
	mysql_select_db(@constant('DB_NAME'));
	$order_by = $_GET['sort'];
	if($order_by != ""){
		$order_by_sort = $order_by;
	}else{
		$order_by_sort = 'desc';
	}
	$ten_sp = $_GET["ten_sp"];
	$loai_sp = $_GET["loai_sp_search"];
	$list_loai_sp = $_GET["list_loai_sp"];
	$page = (isset($_GET["curPage"]) && is_numeric($_GET["curPage"]))?$_GET["curPage"]:1;
	$itemshow = NUM_SP_SEARCH;
	
	$pages = ($page-1)*$itemshow;
	if (isset($_GET["s_a"]) && $_GET["s_a"]==1)
	{
		$sanpham = $_GET["sanpham"];
		$price_from = $_GET["price_from"];
		$price_to = $_GET["price_to"];
		$hangsx = $_GET["hangsx"];
		$sql = "SELECT * FROM ".DB_FIX."sanpham WHERE hien_thi = 1 AND ten_sp LIKE '%".$ten_sp."%'".(strlen($list_loai_sp) || $loai_sp?" AND id_loai_sp IN (".(strlen($list_loai_sp)?$list_loai_sp:($loai_sp?$loai_sp:0)).")":"");
		if(($sanpham != 0) && ($price_from != '') && ($price_to != '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km>= '".$price_from."' and gia_sp_km <= '".$price_to."'";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp>= '".$price_from."' and gia_sp <= '".$price_to."' and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and gia_sp>= '".$price_from."' and gia_sp <= '".$price_to."' and solanban > 0";
			}
		}elseif(($sanpham == 0) && ($price_from != '') && ($price_to != '')){
			$sql .= " and (gia_sp >= '".$price_from."') and (gia_sp <= '".$price_to."')";
		}elseif(($sanpham == 0) && ($price_from != '') && ($price_to == '')){
			$sql .= " and gia_sp >= '".$price_from."'";
		}elseif(($sanpham == 0) && ($price_from == '') && ($price_to != '')){
			$sql .= " and gia_sp = '".$price_to."'";
		}elseif(($sanpham != 0) && ($price_from != '') && ($price_to == '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km = '".$price_from."'";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp >= '".$price_from."' and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and gia_sp >= '".$price_from."' and solanban > 0";
			}
		}elseif(($sanpham != 0) && ($price_from == '') && ($price_to != '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km = '".$price_to."'";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp = '".$price_to."' and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and gia_sp = '".$price_to."' and solanban > 0";
			}
		}elseif(($sanpham != 0) && ($price_from == '') && ($price_to == '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km > 0";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and solanban > 0";
			}
		}
		
		if($hangsx != 0){
			$sql .= " and hang_sx ='".$hangsx."'";
		}
		$sql.=" ORDER BY gia_sp $order_by_sort";
	}
	else
		$sql = "SELECT * FROM ".DB_FIX."sanpham WHERE hien_thi = 1 AND ten_sp LIKE '%".$ten_sp."%'".(strlen($list_loai_sp) || $loai_sp?" AND id_loai_sp IN (".(strlen($list_loai_sp)?$list_loai_sp:($loai_sp?$loai_sp:0)).")":"")." ORDER BY gia_sp $order_by_sort";
	//echo $sql; exit;
	$result = mysql_query($sql);
	$num_rows = mysql_num_rows($result);
if($_GET["s_a"]==1){
?>
<div id="searchAdvanceForm">
<? include_once("../includes/i_timkiem_advance.php");?>
</div>
<?
include_once("../view/v_search_sort_advance.php");
}//end if($_GET["s_a"]==1)
?>
<div>&nbsp;</div>
<form action="" name="frmContent" method="post">
<input type="hidden" name="sortList2" value="<?=$order_by?>" id="sortList2">
<div class="title" style="margin-left:10px; width:10px; height:30px; float:left"></div>
<p style="float:left; line-height:30px; font-weight:bold; color:#FFFFFF; background: url(../images/modul_c.gif);">Kết quả tìm kiếm</p>
<div class="title2" style="width:10px; height:30px; float:left"></div>
<div style="clear:both" class="clear_ie"></div>
<div class="bg_l_t"><div class="bg_r_t"><div class="bg_c_t"></div></div></div>
<div class="cat_sp_cen">
<?php
	if ($num_rows)
	{
		$str_id = "";
		while($row = mysql_fetch_array($result))
			$str_id.= (strlen($str_id)?",":"").$row["id"];
		/*
		if (isset($_GET["s_a"]) && $_GET["s_a"]==1)
		{
			$sql = "SELECT * FROM globe_sanpham WHERE hien_thi = 1 AND ten_sp LIKE '%".$ten_sp."%'".(strlen($list_loai_sp) || $loai_sp?" AND id_loai_sp IN (".(strlen($list_loai_sp)?$list_loai_sp:($loai_sp?$loai_sp:0)).")":"");
		if(($sanpham != 0) && ($price_from != '') && ($price_to != '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km>= '".$price_from."' and gia_sp_km <= '".$price_to."'";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp>= '".$price_from."' and gia_sp <= '".$price_to."' and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and gia_sp>= '".$price_from."' and gia_sp <= '".$price_to."' and solanban > 0";
			}
		}elseif(($sanpham == 0) && ($price_from != '') && ($price_to != '')){
			$sql .= " and (gia_sp >= '".$price_from."') and (gia_sp <= '".$price_to."')";
		}elseif(($sanpham == 0) && ($price_from != '') && ($price_to == '')){
			$sql .= " and gia_sp >= '".$price_from."'";
		}elseif(($sanpham == 0) && ($price_from == '') && ($price_to != '')){
			$sql .= " and gia_sp = '".$price_to."'";
		}elseif(($sanpham != 0) && ($price_from != '') && ($price_to == '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km = '".$price_from."'";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp >= '".$price_from."' and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and gia_sp >= '".$price_from."' and solanban > 0";
			}
		}elseif(($sanpham != 0) && ($price_from == '') && ($price_to != '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km = '".$price_to."'";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp = '".$price_to."' and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and gia_sp = '".$price_to."' and solanban > 0";
			}
		}elseif(($sanpham != 0) && ($price_from == '') && ($price_to == '')){
			if($sanpham == 1){
				$sql .= " and gia_sp_km > 0";
			}elseif($sanpham == 2){
				$sql .= " and gia_sp_km = 0";
			}elseif($sanpham == 3){
				$sql .= " and solanban > 0";
			}
		}
		
		if($hangsx != 0){
			$sql .= " and hang_sx ='".$hangsx."'";
		}
		$sql.=" ORDER BY ngay_dang DESC LIMIT $pages,$itemshow";
		}
		else
			$sql = "SELECT * FROM globe_sanpham WHERE hien_thi = 1 AND ten_sp LIKE '%".$ten_sp."%'".(strlen($list_loai_sp) || $loai_sp?" AND id_loai_sp IN (".(strlen($list_loai_sp)?$list_loai_sp:($loai_sp?$loai_sp:0)).")":"")." ORDER BY ngay_dang DESC LIMIT $pages,$itemshow";
		*/
		$sql = "SELECT * FROM ".DB_FIX."sanpham WHERE id IN (".$str_id.") ORDER BY gia_sp $order_by_sort LIMIT $pages,$itemshow";
		$result = mysql_query($sql);
		//$row = mysql_fetch_array($result);
		//echo $sql; exit;
		$stt = 0;
		
?>
		<table border="0" cellspacing="0" cellpadding="0" width="100%">
<?
		while($row = mysql_fetch_array($result))
		{
			$text = "<table width='300' cellpadding='0' cellspacing='0'><tr><td>".$row['mo_ta_sp']."</td></tr></table>";
			
			echo ++$stt>1?"<tr><td colspan=\"3\"><hr style=\"border-bottom:0.1em solid #FF0000;\" /></td></tr>":"";
?>

<tr>
		<td width="14%" align="center">
			<table  cellpadding="0" cellspacing="0">
				<tr>
					<td align="center" style="padding-bottom:3px; ">
					<?
					$arrayDataHangsx = getById($row['hang_sx']);
					echo strlen($arrayDataHangsx['ten_anh'])?"<img src=\"".DIR_IMAGES_VIEW_HANG_SX.$arrayDataHangsx['ten_anh']."\" width=\"81\" />":"";
					?>
					</td>
				</tr>
				<tr>
					<td style="border:1px solid #E9AE95; border-collapse:collapse;" align="center">
						<a href="?modul=sanpham&action=View&parent_id=<?=$parent_id?>&id_loai_sp=<?=$row['id_loai_sp']?>&id=<?=$row['id']?>">
						<?
						if(!empty($row['anh_sp'])){
							$exists = file_exists(DIR_IMAGES_VIEW.$row["anh_sp"]);
							if($exists){
						?>
						<img src="<?=DIR_IMAGES_VIEW.$row['anh_sp']?>" width="100" border="0" height="100" title="cssheader=[boxover-cssheader] cssbody=[boxover-cssbody] header=[<?=$row['ten_sp']?>] body=[<?=$text?>]" />
						<?
							}else{
						?>
						<img src="../images/no_image_130.jpg"  class="image_thumb_news" width="100" border="0" height="100" title="cssheader=[boxover-cssheader] cssbody=[boxover-cssbody] header=[<?=$row['ten_sp']?>] body=[<?=$text?>]"/>
						<?
							}
						?>
						<?
						}else{
						?>
						<img src="../images/no_image_130.jpg"  class="image_thumb_news" width="100" border="0" height="100" title="cssheader=[boxover-cssheader] cssbody=[boxover-cssbody] header=[<?=$row['ten_sp']?>] body=[<?=$text?>]"/>
						<?
						}
						?>
						
						</a>
					</td>
				</tr>
			</table>		
		</td>
		<td width="65%"  valign="top" style="padding-right:5px; padding-top:3px; padding-left:20px;  ">
			<table cellpadding="0" cellspacing="0" border="0">
				<tr>
					<td class="cat_ten_sp" style="line-height:20px;">
						<a href="?modul=sanpham&action=View&parent_id=<?=$parent_id?>&id_loai_sp=<?=$row['id_loai_sp']?>&id=<?=$row['id']?>">
						<span style="text-transform:uppercase; color:#007DEB; "><?=$row['ten_sp']?></span>
						</a>
					</td>
				</tr>
				 <tr>
					<td style="line-height:20px; padding-right:10px; " align="justify">
						<?=$row['mo_ta_sp']?>
					</td>
				</tr> 
			</table>
		</td> 
		<td width="21%">
		<?
		if($row['gia_sp_km']>0){
		?>
		<span class="ten_gia">Giá cũ:&nbsp;&nbsp;</span><span class="gia_cu"><?=to_money($row['gia_sp'])?> VNĐ </span><br />
		<span class="ten_gia">Chỉ còn:&nbsp;</span><span class="gia_moi"><?=to_money($row['gia_sp_km'])?> VNĐ </span><br /><br />
		
		<?
		}else{
		?>
		<span class="ten_gia">Giá sản phẩm:</span><br /><span class="gia_moi"><?=to_money($row['gia_sp'])?> VNĐ</span><br /><br />
		<?
		}
		?>
		<span class="muangay"><a href="?modul=giohang&action=View&id=<?=$row["id"]?>">Mua ngay</a></span><br />
		<span class="chitiet_sp"><a href="?modul=sanpham&action=View&parent_id=<?=$parent_id?>&id_loai_sp=<?=$row['id_loai_sp']?>&id=<?=$row['id']?>">Chi tiết</a></span>
		</td>
	</tr>

<?php
			
		}
		if ($num_rows>$itemshow){
			$totalPage = (int)($num_rows/$itemshow) + ($num_rows%$itemshow?1:0);
			echo "<tr><td colspan=\"3\"><hr style=\"border-bottom:0.1em solid #FF0000;\" /></td></tr>
				<tr><td colspan=\"3\" align=\"center\"><table><tr>".($page>1?"<td class=\"pagingIntact\"><a href=\"javascript:GotoPage('1')\">&nbsp;&laquo;&nbsp;Trang đầu&nbsp;</a></td>":"");
			for($i=1;$i<= $totalPage;$i++)
				if($i < $page + 11 && $i > $page - 11)
					echo $i == $page?"<td class=\"pagingViewed\">".$i."</td>":"<td class=\"pagingIntact\"><a href=\"javascript:GotoPage('".$i."')\">".$i."</a></td>";			
			echo ($page<$totalPage?"<td class=\"pagingIntact\"><a href=\"javascript:GotoPage('".$totalPage."')\">&nbsp;Trang cuối&nbsp;&raquo;&nbsp;</a></td>":"")."</tr></table></td></tr>";
		}
?>
		</table>
<?
		
	}
	else
		echo "Không tìm thấy kết quả nào.";
?>
</div>
<div class="bg_l_b"><div class="bg_r_b"><div class="bg_c_b"></div></div></div>
</form>
<script type="text/javascript">
	
function GotoPage(iPage) 
{
	var sapxep = document.getElementById("sortList2").value;
	if(sapxep==""){
		sapxep = "desc";
	}
	document.frmContent.action = "?modul=sanpham&action=<?php echo isset($_GET["s_a"]) && $_GET["s_a"]==1?"search_advance_text":"SearchText";?>&sort="+sapxep+"&curPage="+iPage;
	document.frmContent.submit();
}
</script>
</div><!-- resultBasic -->